package com.liuyk.util;

import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.File;
import java.io.FileOutputStream;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

/**
 * 常用工具
 * <p>
 * Created by liuyakui on 2019/7/3.
 */
public class AppUtils {

    public static void changeICONName(File file, String packagename) {
        if (file == null || !file.exists()) {
            return;
        }
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringElementContentWhitespace(true);
        DocumentBuilder db;
        try {
            db = factory.newDocumentBuilder();
            Document doc = db.parse(file);
            //application在改变包名的时候要改为manifest
            NodeList nl = doc.getElementsByTagName("application");
            // 只有一个node，即 manifest
            // item(0)即为 manifest。
            Node node = nl.item(0);
            // 获得manifest的各个属性
            NamedNodeMap map = node.getAttributes();
            // //android:icon在改变包名的时候要改为package,NodeValue要填入你的包名
            map.getNamedItem("android:icon").setNodeValue("@drawable/" + packagename);
            // 写
            saveXml(file, doc);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static void saveXml(File file, Document doc) {
        TransformerFactory transFactory = TransformerFactory.newInstance();
        try {
            Transformer transformer = transFactory.newTransformer();
            transformer.setOutputProperty("indent", "yes");
            DOMSource source = new DOMSource();
            source.setNode(doc);
            StreamResult result = new StreamResult();
            result.setOutputStream(new FileOutputStream(file));
            transformer.transform(source, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getProcessName(Context cxt) {
        int pid = android.os.Process.myPid();
        ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
            if (procInfo.pid == pid) {
                return procInfo.processName;
            }
        }
        return null;
    }

    public static void folderEach(File root) {
        if (root == null || !root.exists()) {
            return;
        }
        File[] files = root.listFiles();
        LinkedList<File> fs = new LinkedList<>();
        for (File file : files) {
            if (file.isDirectory() && !file.isHidden()) {
                fs.add(file);
            } else {
                Log.w("folderEach", file.getAbsolutePath());
            }
        }

        while (!fs.isEmpty()) {
            File file = fs.removeFirst();
            if (file.isDirectory() && !file.isHidden()) {
                File[] listFiles = file.listFiles();
                for (File f : listFiles) {
                    if (f.isDirectory()) {
                        fs.add(f);
                    } else {
                        Log.w("folderEach", f.getAbsolutePath());
                    }
                }
            } else {
                Log.w("folderEach", file.getAbsolutePath());
            }
        }
    }
}
